package dongdongmovie.controller;

import com.dongdongmovie.pojo.PageResult;
import com.dongdongmovie.pojo.TCollection;
import com.dongdongmovie.pojo.TMovie;
import com.dongdongmovie.pojo.VipUser;
import com.dongdongmovie.service.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("TMovie")
public class TMovie_Controller {

    //注入service
    @DubboReference
    private TMovie_Service tMovie_service;
    @Autowired
    private RedisTemplate redisTemplate;
    @DubboReference
    private TCollection_Service tCollection_service;
    @DubboReference
    private VipUser_Service vipUser_service;
    //rocketMQTemplate
    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    //查询最新影视数据
    @ResponseBody
    @RequestMapping("show")
    public List<TMovie> show() {
        List<TMovie> show = tMovie_service.showbydate();
        return show;
    }

    //查询热播影视数据
    @ResponseBody
    @RequestMapping("showrebo")
    public List<TMovie> showrebo(String tName) {
        Set<TMovie> set = redisTemplate.opsForZSet().getOperations().boundZSetOps("shuju").reverseRange(0, 5);
        List<TMovie> list = new ArrayList<>();
        TMovie movie = new TMovie();
        for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
            TMovie t = (TMovie) iterator.next();
            list.add(t);
        }
        return list;
    }

    //查询推荐影视数据
    @ResponseBody
    @RequestMapping("showbytName")
    public List<TMovie> showbytName(String tName) {
        List<TMovie> show = tMovie_service.showbytName(tName);
        return show;
    }

    //查询分类影视数据
    @ResponseBody
    @RequestMapping("showfenlei")
    public PageResult<TMovie> showfenlei(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        Integer id = (Integer) request.getSession().getAttribute("fenlei");
        if (id == 9999) {
            VipUser vipUser = (VipUser) request.getSession().getAttribute("VipUser");
            VipUser vipUser1 = vipUser_service.showuserid(vipUser.getUsername());
            return tCollection_service.show(vipUser1.getId(), pageNum, pageSize);
        }
        TMovie tMovie = new TMovie();
        tMovie.setChannel(id.toString());
        PageResult<TMovie> show = tMovie_service.showbyIdall(tMovie, pageNum, pageSize);
        return show;
    }

    //查询正常过滤后数据
    @ResponseBody
    @RequestMapping("showfenleizhengchang")
    public PageResult<TMovie> showfenleizhengchang(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        Integer id = (Integer) request.getSession().getAttribute("fenlei");
        Integer typeid = (Integer) request.getSession().getAttribute("typeid");
        Integer areaid = (Integer) request.getSession().getAttribute("areaid");
        Integer yearsid = (Integer) request.getSession().getAttribute("yearsid");
        TMovie tMovie = new TMovie();
        tMovie.setChannel(id.toString());
        tMovie.setType(typeid);
        tMovie.setArea(areaid);
        tMovie.setYears(yearsid);
        PageResult<TMovie> show = tMovie_service.showbyIdall(tMovie, pageNum, pageSize);
        return show;
    }

    //查询按时间排序后数据
    @ResponseBody
    @RequestMapping("showfenleishijian")
    public PageResult<TMovie> showfenleishijian(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        Integer id = (Integer) request.getSession().getAttribute("fenlei");
        Integer typeid = (Integer) request.getSession().getAttribute("typeid");
        Integer areaid = (Integer) request.getSession().getAttribute("areaid");
        Integer yearsid = (Integer) request.getSession().getAttribute("yearsid");
        TMovie tMovie = new TMovie();
        tMovie.setChannel(id.toString());
        tMovie.setType(typeid);
        tMovie.setArea(areaid);
        tMovie.setYears(yearsid);
        PageResult<TMovie> show = tMovie_service.showbyIdallshijian(tMovie, pageNum, pageSize);
        return show;
    }

    //按评分排序
    @ResponseBody
    @RequestMapping("showfenleipingfen")
    public PageResult<TMovie> showfenleipingfen(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        Integer id = (Integer) request.getSession().getAttribute("fenlei");
        Integer typeid = (Integer) request.getSession().getAttribute("typeid");
        Integer areaid = (Integer) request.getSession().getAttribute("areaid");
        Integer yearsid = (Integer) request.getSession().getAttribute("yearsid");
        TMovie tMovie = new TMovie();
        tMovie.setChannel(id.toString());
        tMovie.setType(typeid);
        tMovie.setArea(areaid);
        tMovie.setYears(yearsid);
        PageResult<TMovie> show = tMovie_service.showbyIdallpingfen(tMovie, pageNum, pageSize);
        return show;
    }

    //根据搜索查询数据
    @ResponseBody
    @RequestMapping("sousuoba")
    public PageResult<TMovie> sousuoba(String movieName, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        TMovie tMovie = new TMovie();
        tMovie.setMovieName(movieName);
        PageResult<TMovie> show = tMovie_service.showbyIdallpingfens(tMovie, pageNum, pageSize);
        return show;
    }

    //添加收藏
    @ResponseBody
    @RequestMapping("shoucang")
    public int shoucang(Integer id, HttpServletRequest request) {
        VipUser vipUser = (VipUser) request.getSession().getAttribute("VipUser");
        if (vipUser == null) {
            return 0;
        }
        VipUser vipUser1 = vipUser_service.showuserid(vipUser.getUsername());
        TCollection tCollection = new TCollection();
        tCollection.setMovieId(id);
        tCollection.setUserId(vipUser1.getId());
        rocketMQTemplate.convertAndSend("ggw", "收藏成功");
        return tCollection_service.add(tCollection);
    }
}
